package com.xhb.ui.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhb.biz.po.CollectGroupPO;
import com.xhb.biz.po.CollectUrlPO;
import com.xhb.biz.service.CollectGroupService;
import com.xhb.biz.service.CollectUrlService;
import lombok.Data;
import org.noear.solon.Solon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Data
public class UpdateCollectUrlDialog extends JDialog {

    private JPanel jPanel;

    private CollectUrlPO collectUrlPO;

    private CollectBar collectBar;

    private JComboBox comboBox;

    public UpdateCollectUrlDialog(CollectUrlPO collectUrlPO, CollectBar collectBar, Boolean delFlag) {
        this.collectUrlPO = collectUrlPO;
        this.collectBar = collectBar;

        //加载收藏夹文件夹
        CollectGroupService collectGroupService = Solon.context().getBean(CollectGroupService.class);
        List<CollectGroupPO> collectGroupPOS = collectGroupService.list(new LambdaQueryWrapper<CollectGroupPO>().orderByAsc(CollectGroupPO::getSortNo));
        LinkedHashMap<String, String> groupMap = new LinkedHashMap<>();
        for (CollectGroupPO collectGroupPO : collectGroupPOS) {
            groupMap.put(collectGroupPO.getName(), collectGroupPO.getId());
        }
        this.collectUrlPO.setGroupMap(groupMap);

        //去掉边框
        //setUndecorated(true);
        //getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
        //设置窗体宽高
        setSize(400, 200);
        //设置关闭窗口结束程序
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //设置窗体禁止调节大小
        setResizable(false);
        //居中
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);
        if (delFlag) {
            add(delCollectPanel());
        } else {
            add(createCollectPanel());
        }
    }

    public JPanel delCollectPanel() {
        CollectUrlService bean = Solon.context().getBean(CollectUrlService.class);
        if (null == collectUrlPO.getId()) {
//            String urlId = IdUtil.simpleUUID();
//            collectUrlPO.setId(urlId);
            CollectUrlPO addPo = CollectUrlPO.builder()
//                    .id(urlId)
                    .name(collectUrlPO.getName())
                    .url(collectUrlPO.getUrl())
                    .build();
            bean.insert(addPo);
        }

        //创建面板
        JPanel jPanel = new JPanel();
        //布局设置为空，之后可以手动设置组件的坐标位置和大小
        jPanel.setLayout(null);
        JLabel name = new JLabel("名称:");
        name.setFont(new Font("微软雅黑", 0, 13));
        //定义组件的位置和宽高
        name.setBounds(10, 20, 100, 25);
        //把组件添加到JPanel上
        jPanel.add(name);

        JTextField nameValue = new JTextField(30);
        //设置文本域的位置和宽高
        nameValue.setBounds(100, 20, 210, 25);
        nameValue.setText(collectUrlPO.getName());
        //把文本域组件添加上
        jPanel.add(nameValue);

        JLabel group = new JLabel("文件夹:");
        group.setFont(new Font("微软雅黑", 0, 13));
        group.setBounds(10, 60, 100, 25);
        jPanel.add(group);

        // 创建下拉框
        comboBox = new JComboBox();
        // 绑定下拉框选项
        comboBox.addItem(null);
        LinkedHashMap<String, String> groupMap = collectUrlPO.getGroupMap();
        if (null != groupMap) {
            for (String groupName : groupMap.keySet()) {
                comboBox.addItem(groupName);
                if (groupMap.get(groupName).equals(collectUrlPO.getCollectGroupId())) {
                    comboBox.setSelectedItem(groupName);
                }
            }
        }
        comboBox.setFont(new Font("宋体", Font.PLAIN, 13));
        comboBox.setBounds(100, 60, 210, 25);
        jPanel.add(comboBox);

        JButton save = new JButton("保存");
        save.setBounds(80, 100, 100, 25);

        JButton register = new JButton("删除");
        register.setBounds(220, 100, 100, 25);
        register.addActionListener(event -> {
            bean.deleteAsync(collectUrlPO.getId());
            setVisible(false);
        });
        jPanel.add(register);
        jPanel.add(save);

        SaveListener ll = new SaveListener(this, collectUrlPO.getId(), nameValue, null);
        save.addActionListener(ll);

        ReListener re = new ReListener();
        register.addActionListener(re);
        return jPanel;
    }


    public JPanel createCollectPanel() {
        //创建面板
        JPanel jPanel = new JPanel();
        //布局设置为空，之后可以手动设置组件的坐标位置和大小
        jPanel.setLayout(null);
        JLabel name = new JLabel("名称:");
        name.setFont(new Font("微软雅黑", 0, 13));
        //定义组件的位置和宽高
        name.setBounds(10, 20, 100, 25);
        //把组件添加到JPanel上
        jPanel.add(name);

        JTextField nameValue = new JTextField(30);
        //设置文本域的位置和宽高
        nameValue.setBounds(100, 20, 210, 25);
        nameValue.setText(collectUrlPO.getName());
        //把文本域组件添加上
        jPanel.add(nameValue);

        JLabel url = new JLabel("网址:");
        url.setFont(new Font("微软雅黑", 0, 13));
        url.setBounds(10, 50, 100, 25);
        jPanel.add(url);

        JTextField urlValue = new JTextField(20);
        urlValue.setBounds(100, 50, 210, 25);
        urlValue.setText(collectUrlPO.getUrl());
        jPanel.add(urlValue);

        JLabel group = new JLabel("文件夹:");
        group.setFont(new Font("微软雅黑", 0, 13));
        group.setBounds(10, 80, 100, 25);
        jPanel.add(group);

        // 创建下拉框
        comboBox = new JComboBox();
        // 绑定下拉框选项
        comboBox.addItem(null);
        LinkedHashMap<String, String> groupMap = collectUrlPO.getGroupMap();
        if (null != groupMap) {
            for (String groupName : groupMap.keySet()) {
                comboBox.addItem(groupName);
                if (groupMap.get(groupName).equals(collectUrlPO.getCollectGroupId())) {
                    comboBox.setSelectedItem(groupName);
                }
            }
        }
        comboBox.setFont(new Font("宋体", Font.PLAIN, 13));
        comboBox.setBounds(100, 80, 210, 25);
        jPanel.add(comboBox);

        JButton save = new JButton("保存");
        save.setBounds(80, 120, 100, 25);

        JButton register = new JButton("取消");
        register.setBounds(220, 120, 100, 25);
        register.addActionListener(event -> {
            setVisible(false);
        });
        jPanel.add(register);
        jPanel.add(save);

        SaveListener ll = new SaveListener(this, collectUrlPO.getId(), nameValue, urlValue);
        save.addActionListener(ll);

        ReListener re = new ReListener();
        register.addActionListener(re);
        return jPanel;
    }

    public class ReListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
        }
    }

    public class SaveListener implements ActionListener {
        private javax.swing.JTextField nameValue;
        private javax.swing.JTextField urlValue;
        private javax.swing.JDialog jDialog;//定义一个窗体对象

        private String id;

        public SaveListener(javax.swing.JDialog jDialog, String id, javax.swing.JTextField nameValue, javax.swing.JTextField urlValue) {
            //获取界面
            this.jDialog = jDialog;
            this.id = id;
            //获取界面中的输入框对象
            this.nameValue = nameValue;
            this.urlValue = urlValue;
        }

        public void actionPerformed(ActionEvent e) {
            String groupId = null;
            if (null != comboBox.getSelectedItem()) {
                String groupName = comboBox.getSelectedItem().toString();
                Map<String, String> groupMap = collectUrlPO.getGroupMap();
                if (groupMap.containsKey(groupName)) {
                    groupId = groupMap.get(groupName);
                }
            }
            String url = collectUrlPO.getUrl();
            if (null != urlValue) {
                url = urlValue.getText();
            }
            CollectUrlService bean = Solon.context().getBean(CollectUrlService.class);
            if (null == id) {
                CollectUrlPO addPo = CollectUrlPO.builder()
                        .name(nameValue.getText())
                        .url(url)
                        .collectGroupId(groupId)
                        .build();
                bean.insert(addPo);
            } else {
                CollectUrlPO updatePO = CollectUrlPO.builder()
                        .id(id)
                        .name(nameValue.getText())
                        .url(url)
                        .collectGroupId(groupId)
                        .icon(collectUrlPO.getIcon())
                        .build();
                bean.updateAsync(updatePO);
            }
            setVisible(false);
            collectBar.refresh();
        }
    }
}
